package com.fengling.api.config;

import com.fengling.common.utils.UserContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//openfeign的请求拦截器 在请求头中设置传递的用户信息
@Configuration
public class FeignConfig {
    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                Long userId = UserContext.getUser();
                if(userId!=null){
                    template.header("user-info",userId.toString());
                }
            }
        };
    }
}
